package com.ly.heart.service.impl;

import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ly.common.core.domain.AjaxResult;
import com.ly.common.core.domain.entity.SysDept;
import com.ly.common.utils.DateUtils;
import com.ly.common.utils.StringUtils;
import com.ly.common.utils.erp.ConstUtil;
import com.ly.common.utils.erp.SnowflakeIdWorker;
import com.ly.heart.domain.KingTravelTripEntity;
import com.ly.heart.service.IKingTravelDetailService;
import com.ly.heart.service.IKingTravelTripService;
import com.ly.heart.util.ChineseToPinyin;
import com.ly.heart.util.CurrentUserUtil;
import com.ly.system.service.ISysDeptService;
import org.springframework.stereotype.Service;
import com.ly.heart.mapper.KingTravelBusinessReimbursementMapper;
import com.ly.heart.domain.KingTravelBusinessReimbursementEntity;
import com.ly.heart.service.IKingTravelBusinessReimbursementService;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;

/**
 * 差旅费报销单Service业务层处理
 * 
 * @author wutao
 * @date 2024-08-23
 */
@Service
public class KingTravelBusinessReimbursementServiceImpl extends ServiceImpl<KingTravelBusinessReimbursementMapper,KingTravelBusinessReimbursementEntity> implements IKingTravelBusinessReimbursementService {

    @Resource
    private KingTravelBusinessReimbursementMapper kingTravelBusinessReimbursementEntityMapper;
    @Resource
    private ISysDeptService sysDeptService;
    @Resource
    private IKingTravelTripService kingTravelTripService;
    @Resource
    private IKingTravelDetailService kingTravelDetailService;

    /**
     * 查询差旅费报销单
     * 
     * @param id 差旅费报销单ID
     * @return 差旅费报销单
     */
    @Override
    public KingTravelBusinessReimbursementEntity selectKingTravelBusinessReimbursementEntityById(String id)
    {
        return kingTravelBusinessReimbursementEntityMapper.selectKingTravelBusinessReimbursementEntityById(id);
    }

    /**
     * 查询差旅费报销单列表
     * 
     * @param kingTravelBusinessReimbursementEntity 差旅费报销单
     * @return 差旅费报销单
     */
    @Override
    public List<KingTravelBusinessReimbursementEntity> selectKingTravelBusinessReimbursementEntityList(KingTravelBusinessReimbursementEntity kingTravelBusinessReimbursementEntity)
    {
        return kingTravelBusinessReimbursementEntityMapper.selectKingTravelBusinessReimbursementEntityList(kingTravelBusinessReimbursementEntity);
    }

    /**
     * 新增差旅费报销单
     * 
     * @param kingTravelBusinessReimbursementEntity 差旅费报销单
     * @return 结果
     */
    @Override
    public int insertKingTravelBusinessReimbursementEntity(KingTravelBusinessReimbursementEntity kingTravelBusinessReimbursementEntity)
    {
        kingTravelBusinessReimbursementEntity.setCreateTime(DateUtils.getNowDate());
        return kingTravelBusinessReimbursementEntityMapper.insertKingTravelBusinessReimbursementEntity(kingTravelBusinessReimbursementEntity);
    }

    /**
     * 修改差旅费报销单
     * 
     * @param kingTravelBusinessReimbursementEntity 差旅费报销单
     * @return 结果
     */
    @Override
    public int updateKingTravelBusinessReimbursementEntity(KingTravelBusinessReimbursementEntity kingTravelBusinessReimbursementEntity)
    {
        kingTravelBusinessReimbursementEntity.setUpdateTime(DateUtils.getNowDate());
        return kingTravelBusinessReimbursementEntityMapper.updateKingTravelBusinessReimbursementEntity(kingTravelBusinessReimbursementEntity);
    }

    /**
     * 批量删除差旅费报销单
     * 
     * @param ids 需要删除的差旅费报销单ID
     * @return 结果
     */
    @Override
    public int deleteKingTravelBusinessReimbursementEntityByIds(String[] ids)
    {
        return kingTravelBusinessReimbursementEntityMapper.deleteKingTravelBusinessReimbursementEntityByIds(ids);
    }

    /**
     * 删除差旅费报销单信息
     * 
     * @param id 差旅费报销单ID
     * @return 结果
     */
    @Override
    public int deleteKingTravelBusinessReimbursementEntityById(String id)
    {
        return kingTravelBusinessReimbursementEntityMapper.deleteKingTravelBusinessReimbursementEntityById(id);
    }

    @Override
    public AjaxResult draft(KingTravelBusinessReimbursementEntity kingTravelBusinessReimbursementEntity) {
        String id = SnowflakeIdWorker.getInstance().nextStringId();
        Date nowDate = DateUtils.getNowDate();
        kingTravelBusinessReimbursementEntity.setId(id).setCreateTime(nowDate);
        if (StringUtils.isEmpty(kingTravelBusinessReimbursementEntity.getDocCode())){
            String companyId = CurrentUserUtil.getCompanyId();
            SysDept sysDept = sysDeptService.getById(companyId);
            int count = this.count();
            String countStr = String.format("%06d", count);
            String firstLetter = ChineseToPinyin.getFirstLetter(sysDept.getDeptName());
            String docCode = firstLetter.concat("_CLFBXD_").concat(countStr);
            kingTravelBusinessReimbursementEntity.setDocCode(docCode);
        }
        if (!CollectionUtils.isEmpty(kingTravelBusinessReimbursementEntity.getKingTravelTripEntityList())){
            kingTravelBusinessReimbursementEntity.getKingTravelTripEntityList().forEach(kingTravelTripEntity -> {
                kingTravelTripEntity.setId(SnowflakeIdWorker.getInstance().nextStringId());
                kingTravelTripEntity.setCreateTime(nowDate);
                kingTravelTripEntity.setBusinessId(id);
                kingTravelTripEntity.setBusinessCode(kingTravelBusinessReimbursementEntity.getDocCode());
                if (!CollectionUtils.isEmpty(kingTravelTripEntity.getTripDetailList())){
                    kingTravelTripEntity.getTripDetailList().forEach(kingTravelDetailEntity -> {
                        kingTravelDetailEntity.setId(SnowflakeIdWorker.getInstance().nextStringId());
                        kingTravelDetailEntity.setCreateTime(nowDate);
                        kingTravelDetailEntity.setTravelId(id);
                        kingTravelDetailEntity.setTravelTripId(kingTravelTripEntity.getId());
                    });
                    kingTravelDetailService.saveBatch(kingTravelTripEntity.getTripDetailList());
                }
            });
            boolean saved = kingTravelTripService.saveBatch(kingTravelBusinessReimbursementEntity.getKingTravelTripEntityList());
            if (!saved){
                return AjaxResult.error("暂存出差行程失败");
            }
        }
        boolean save = this.save(kingTravelBusinessReimbursementEntity);
        if (!save){
            return AjaxResult.error("暂存差旅费报销单失败");
        }
        return AjaxResult.success("暂存差旅费报销单成功");
    }

    @Override
    public AjaxResult submit(KingTravelBusinessReimbursementEntity kingTravelBusinessReimbursementEntity) {
        Date nowDate = DateUtils.getNowDate();
        String id = kingTravelBusinessReimbursementEntity.getId();
        if (StringUtils.isEmpty(id)){
            return AjaxResult.error("请先暂存该单");
        }
        if (!CollectionUtils.isEmpty(kingTravelBusinessReimbursementEntity.getKingTravelTripEntityList())){
            boolean remove = kingTravelTripService.remove(new LambdaQueryWrapper<>(KingTravelTripEntity.class).eq(KingTravelTripEntity::getBusinessId, kingTravelBusinessReimbursementEntity.getId()));
            if (!CollectionUtils.isEmpty(kingTravelBusinessReimbursementEntity.getKingTravelTripEntityList())){
                kingTravelBusinessReimbursementEntity.getKingTravelTripEntityList().forEach(kingTravelTripEntity -> {
                    kingTravelTripEntity.setId(SnowflakeIdWorker.getInstance().nextStringId());
                    kingTravelTripEntity.setCreateTime(nowDate);
                    kingTravelTripEntity.setBusinessId(id);
                    kingTravelTripEntity.setBusinessCode(kingTravelBusinessReimbursementEntity.getDocCode());
                    if (!CollectionUtils.isEmpty(kingTravelTripEntity.getTripDetailList())){
                        kingTravelTripEntity.getTripDetailList().forEach(kingTravelDetailEntity -> {
                            if (StringUtils.isNotEmpty(kingTravelDetailEntity.getTravelType()) &&
                                    (ConstUtil.TRAVEL_TRAFFIC_TYPE_AIR.equals(kingTravelDetailEntity.getTravelType())
                                            || ConstUtil.TRAVEL_TRAFFIC_TYPE_RAIL.equals(kingTravelDetailEntity.getTravelType())
                                            || ConstUtil.TRAVEL_TRAFFIC_TYPE_SHIP.equals(kingTravelDetailEntity.getTravelType()))){



                            }
                            //判断明细信息：差旅类型为铁路车票(中国)、航空运输(中国)、轮船时，增加座位等级字段，
                            // 在保存是判断交通工具座位等级和住宿及差旅补助【（结束日期-开始日期）*标准/天】有没有超标，如果超标，提醒哪项超标，然后基本信息增加超标原因需要填写。
                            //todo 疑问住宿及差旅补助明细表有选择地域，差旅类型，以及标准/天;判断交通工具座位等级这个可以判断，
                            // 住宿及差旅补助【（结束日期-开始日期）*标准/天】有没有超标，这个标准/天在明细里面，怎么与行程单的行程关联起来？
                        });
                    }
                });
                boolean saved = kingTravelTripService.saveBatch(kingTravelBusinessReimbursementEntity.getKingTravelTripEntityList());
                if (!saved){
                    return AjaxResult.error("暂存出差行程失败");
                }
            }

        }
        return null;
    }
}
